<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="80px" size="mini">
      <el-form-item label="所属年" prop="year">
        <el-date-picker
          v-model="formItem.year"
          type="year"
          value-format="yyyy"
          placeholder="请选择所属年"
        />
      </el-form-item>
      <el-form-item v-if="isWorkerGroupLeader" label="运维小组" prop="groupCode">
        <el-select v-model="formItem.groupCode" placeholder="请选择运维小组" style="width:100%">
          <el-option v-for="item in availableOperationGroups" :key="item.code" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务标题" prop="title">
        <el-input v-model="formItem.title" placeholder="请输入任务标题" />
      </el-form-item>
      <el-form-item label="任务类型" prop="type">
        <el-select v-model="formItem.type" placeholder="任务类型" style="width:100%">
          <el-option label="年中总结" value="MIDDLE_YEAR_SUMMARY" />
          <el-option label="年末总结" value="END_YEAR_SUMMARY" />
        </el-select>
      </el-form-item>
      <el-form-item label="截止时间" prop="closedAt">
        <el-date-picker v-model="formItem.closedAt" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="截止时间" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import moment from 'moment'
import { mapGetters } from 'vuex'
import { formatDate, weekEnd } from '@neu/biz-core/utils/utils'
import { detail, create, update } from '@neu/dcom-ui/api/questionnaire'
import { list as operationGroupList } from '@neu/dcom-ui/api/operationGroup'

// 定义一个表单原始项
const formItem = {
  groupCode: '',
  title: '',
  type: 'MIDDLE_YEAR_SUMMARY',
  scope: '',
  year: moment().year() + '',
  closedAt: formatDate(weekEnd(new Date()))
}
export default {
  name: 'QuestionnaireForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        groupCode: [{ required: true, message: '运维小组不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '任务标题不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '任务类型不能为空', trigger: 'blur' }],
        closedAt: [{ required: true, message: '截止时间不能为空', trigger: 'blur' }]
      },
      availableOperationGroups: []
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ]),
    isWorkerGroupLeader: function() {
      return this.roles.some(r => r === 'workerGroupLeader')
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
    operationGroupList().then(response => {
      this.availableOperationGroups = response.data
    })
    this.formItem.groupCode = this.$store.getters['dcom/projectUserInfo'].groupCode
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      if (this.isWorkerGroupLeader) {
        this.formItem.scope = 'WORKER'
      } else {
        this.formItem.scope = 'LEADER'
        this.formItem.groupCode = 'nouse'
      }
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
